home *** CD-ROM | disk | FTP | other *** search
- Path: locutus.rchland.ibm.com!usenet
- From: pstaite@vnet.ibm.com
- Newsgroups: comp.lang.c++
- Subject: Re: Reading string from text file
- Date: 5 Jan 1996 15:27:28 GMT
- Organization: IBM OS/2 Device Driver Development Rochester, MN
- Message-ID: <4cjg10$16vn@locutus.rchland.ibm.com>
- References: <4cie21$8vk@daily-planet.nodak.edu>
- Reply-To: pstaite@vnet.ibm.com
- NNTP-Posting-Host: warpone.rchland.ibm.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4cie21$8vk@daily-planet.nodak.edu>, dhill@badlands.NoDak.edu (Dale A Hill) writes:
- >Looking for code sample that searches a text file line by line, searches for
- >a particular text string on each line...if it finds it, marks its
- >location and extracts (reads) all data from beginning of line to the
- >search string -1. Any help would be greatly appreciated.
-
- Too simple for words, so here's code (untested) :-)
-
- #include<iostream.h>
- #include<fstream.h>
- #include<string.h>
-
- const size_t maxbuf = 4096;
-
- int main( int agrc, char* argv[] ) {
- if( argc < 3 ) {
- cerr << "Hey, give me a filename and a string!" << endl;
- return -1; }
- ifstream f( argv[ 1 ] );
- if( ! f ) {
- cerr << "Hey, couldn't open file: " << argv[ 1 ] << endl;
- return -2; }
- char* buf( new char[ maxbuf ] );
- if( ! buf ) {
- cerr << "Can't even get " << maxbuf << " bytes!" << endl;
- return -3; }
- while( f.getline( buf, maxbuf ) ) {
- char* p( strstr( buf, argv[ 2 ] ) );
- if( p ) {
- *p = '\0';
- cout << buf << endl; } }
- delete[] buf;
- return 0; }
-
-
-
- Phil Staite, team OS/2
- internet: pstaite@vnet.ibm.com internal: pstaite@rchland
-
-